<?PHP



class LoginModel {
    
    // Save the temp-users in an array
    private $users = array(
        "user" => "5f4dcc3b5aa765d61d8327deb882cf99",
        "user2" => "c1572d05424d0ecb2a65ec6a82aeacbf"
    );


    const sessionUsernameName = "username" ;

    /**
    * IsLoggedIn
    * 
    * Function to check if user is logged in
    * 
    * @return bool | return true if user is logged in, else false
    */
    public function isLoggedIn() {

        // If the session userName is set, then a user is logged in        
        if (isset($_SESSION[$this::sessionUsernameName])) {
            return true ;
        }

        return false ;
    }
    /**
    * Function to handle the login
    *
    * @return bool | true if the login succeeded and false if not
    */
    public function doLogin($username, $password) {

        
        // Check if the username entered is in the users-list and then if the password matches.
        if(isset($this->users[$username]) && $this->users[$username] === $password) {

            // Save the username as a session so we can see that the user have logged in
            $_SESSION[$this::sessionUsernameName] = $username ;

            return true ;
        }
        
        return false ;
    }
    /** 
    * Handles the logout
    * @return bool | true if the login succeded and false if not
    */
    public function doLogout() {
        // Unset the session
        unset($_SESSION[$this::sessionUsernameName]) ;

        // Check so the session has been removed
        if(isset($_SESSION[$this::sessionUsernameName]) == false) {
            return true ;
        }

        return false ;
    }
    
    /**
     * CheckUsername 
     *
     * Check so that username exists, could be used in error-handling 
     *
     * @param string $username 
     * @access public
     * @return bool
     */
    public function CheckUsername($username) {
        return (isset($this->users[$username])) ? true : false ;
    }
}

?>
